/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.leadiv.ai.minesweeper.util;

import java.io.*;
import java.lang.reflect.Field;
import java.util.Enumeration;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author pborrego
 */
public class Params {
    public int iFramesPerSecond = 0;
    public static int iNumInputs = 0;
    public static int iNumHidden = 0;
    public int iNeuronsPerHiddenLayer = 0;
    public int iNumOutputs = 0;
    public double dActivationResponse = 0;
    public double dBias = 0;
    public double dMaxTurnRate = 0;
    public double dMaxSpeed = 0;
    public int iSweeperScale = 0;
    public int iNumMines = 0;
    public int iNumSweepers = 0;
    public int iNumTicks = 0;
    public double dMineScale = 0;
    public double dCrossoverRate = 0;
    public double dMutationRate = 0;
    public double dMaxPerturbation = 0;
    public int iNumElite = 0;
    public int iNumCopiesElite = 0;
    public double dPi = 3.14159265358979;
    public double dHalfPi = dPi / 2;
    public double dTwoPi = dPi * 2;
    public int iWindowWidth = 400;
    public int iWindowHeight = 400;

    private Properties props;

    public Params() { }

    public Params(String propsFile) {
        try {
            ClassLoader loader = this.getClass().getClassLoader();
            InputStream in = loader.getResourceAsStream(propsFile);
            props = new Properties();
            props.load(in);

            Enumeration e = props.propertyNames();
            while(e.hasMoreElements()) {
                this.setProperty((String)e.nextElement());
            }

        } catch (IOException ex) {
            Logger.getLogger(Params.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private int getIntProperty(String prop, int dflt) {
        return Integer.parseInt(props.getProperty(prop, Integer.toString(dflt)));
    }

    private double getDoubleProperty(String prop, double dflt) {
        return Double.parseDouble(props.getProperty(prop, Double.toString(dflt)));
    }

    private void setProperty(String prop) {
        try {
            Params p = this;
            Field f = this.getClass().getField(prop);
            String t = f.getType().getName();

            if(t.equals("int")) {
                int i = f.getInt(p);
                f.setInt(p, this.getIntProperty(prop, i));
            }

            if(t.equals("double")) {
                f.setDouble(p, this.getDoubleProperty(prop, f.getDouble(p)));
            }
        } catch (NoSuchFieldException ex) {
            Logger.getLogger(Params.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(Params.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalArgumentException ex) {
            Logger.getLogger(Params.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}